package com.ccx.coachsite.ui.customview.switchview;



import java.util.ArrayList;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;

import com.ccx.coachsite.R;
import com.ccx.coachsite.downloadmgr.AsyncImageLoaderDetail;
import com.ccx.coachsite.downloadmgr.AsyncImageLoaderDetail.ImageCallback;
import com.ccx.coachsite.ui.customview.switchview.MyViewGroup.OnClickCallback;



/**
 * 主题高清大图预览窗口
 * @author cfb
 *
 */
/*
 * 使用方法
//如果多张预览图 启用高清大图滑屏预览模式	
Intent intent = new Intent( mContext, ScrollPreviewImgActivity.class );
intent.putStringArrayListExtra( "imageUrlList", imageUrlList ); //图片列表
intent.putExtra("curImagePostion", position);					//进入时显示第几张图片
mContext.startActivity( intent );
*/
public class ScrollPreviewImgActivity extends Activity implements OnClickCallback {

	private MyViewGroup viewGroup;

	private PageControlView pageControl;

	private AsyncImageLoaderDetail mAsyncImageLoader = new AsyncImageLoaderDetail();
	
	private int initPos = 0; 
	private boolean hasInitFlag = false;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,WindowManager.LayoutParams. FLAG_FULLSCREEN);
		
		setContentView(R.layout.scrollpreview);
		viewGroup = (MyViewGroup) findViewById(R.id.myViewGroup);

		initPos = getIntent().getIntExtra("curImagePostion", 0);
		ArrayList<String> imageUrlList = getIntent().getStringArrayListExtra("imageUrlList");

		for (int i = 0; i < imageUrlList.size(); i++) {
			ImageView imageView = new ImageView(this);
			String imageUrl=imageUrlList.get(i);
			imageView.setTag(imageUrl);
			viewGroup.addView(imageView);
			
			Drawable cachedImage = mAsyncImageLoader.loadDrawable(imageUrl, new ImageCallback() {
	            public void imageLoaded(Drawable imageDrawable, String imageUrl) {
	            	if ( viewGroup ==null )return; 
	                ImageView imageViewByTag = (ImageView) viewGroup.findViewWithTag(imageUrl);
	                
	                if (imageViewByTag != null && imageDrawable!=null) {
	                    imageViewByTag.setImageDrawable(imageDrawable);
	                    imageViewByTag.setTag(R.id.large_image_load_success, "true");
	                    AlphaAnimation aa = new AlphaAnimation(0.5f, 1.0f);
	                    aa.setDuration(500);
	                    imageViewByTag.startAnimation(aa);	
	                }	                
	            }
	        });
			
			viewGroup.setOnClickCallback(this);
			
			if (cachedImage == null) {
				imageView.setImageResource(R.drawable.no_find_small);
				imageView.setTag(R.id.large_image_load_success, "false");
			}else{						
				imageView.setImageDrawable(cachedImage);	
				imageView.setTag(R.id.large_image_load_success, "true");
			}				
		}		
		
		pageControl=(PageControlView) findViewById(R.id.pageControl);
		pageControl.setCount(viewGroup.getChildCount());
		pageControl.generatePageControl(initPos);		
		viewGroup.setScrollToScreenCallback(pageControl);		
	}
	
	public void onWindowFocusChanged(boolean hasFocus) {
		if (hasFocus && !hasInitFlag) {
			hasInitFlag = true;
			viewGroup.scrollToScreen(initPos);
		}
	};
	
	@Override
	protected void onDestroy() {

		super.onDestroy();
		//释放Image资源
		
		if (viewGroup==null) return;
		
  	    for (int i = 0; i < viewGroup.getChildCount(); ++i){
          	View v = viewGroup.getChildAt(i);
          	if (v!=null && v instanceof ImageView) {
          		ImageView imgView = (ImageView)v;
          		Drawable drawable = imgView.getDrawable();
          		
          		if (drawable!=null) {          			
          			drawable.setCallback( null );          			
          		}
          	}          	
  	    }

  	    viewGroup.removeAllViews();
  	  
    	System.gc();    	
	}

	@Override
	public void clickCallback() {
		// TODO Auto-generated method stub
		finish();
	}
}